<template>
  <div class="app-container">
    <div class="search-container">
      <el-form :model="searchParam" :inline="true">
        <el-form-item label="采集时间">
          <el-date-picker
              v-model="searchParam.collectTimeSearch"
              type="datetimerange"
              range-separator="至"
              start-placeholder="开始日期"
              end-placeholder="结束日期"
              value-format="YYYY-MM-DD HH:mm:ss" @change="getcollectTime">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="数据文件名称">
                      <el-input
                    v-model="searchParam.collectFilename"
                    placeholder="请输入数据文件名称"
                    clearable
                    @keyup.enter="search"
            />
                  </el-form-item>
        <el-form-item label="采集状态">
          <el-select v-model="searchParam.collectStatus" placeholder="请选择采集状态" clearable>
            <el-option
              v-for="item in useDictionary('FileReadStatus')"
              :key="item.value"
              :label="item.label"
              :value="item.value"
            />
          </el-select>
                  </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="search"><i-ep-search />搜索</el-button>
          <el-button @click="reset"><i-ep-refresh />重置</el-button>
        </el-form-item>
      </el-form>
    </div>

    <el-card shadow="never" class="table-container">
      <template #header>
        <el-button type="success" @click="excelBoxOpen">导入 </el-button>
        <el-dialog
          :close-on-click-modal="false"
          title="SAP入口校验数据导入"
          v-model="excelBox"
          width="555px"
          :key="uploadKey"
        >
          <div>
            <el-button
              @click="
                useDownload(
                  '/pqd-etl/sapeidcollectrecord/exportTemp',
                  'SAP入口校验上传模板',
                  searchParam
                )
              "
            >
              <template #icon>
                <i-ep-download />
              </template>
              SAP入口校验上传模板下载
            </el-button>
          </div>
          <div style="margin-top: 10px">
            <input
              class="crop-input"
              type="file"
              name="image"
              accept="application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"
              ref="onload_exl"
              @change="getfilepath"
            />
            <el-button
              @click="upload"
              :disabled="uploadBtn"
              style="cursor: pointer"
            >上传</el-button
            >
          </div>
        </el-dialog>
      </template>

      <el-table
          ref="dataTableRef"
          v-loading="loading"
          :data="tableData"
          highlight-current-row
          border
          @selection-change="handleSelectionChange">
<!--      <el-table-column type="selection" width="55" align="center" />-->
      <el-table-column label="采集时间" prop="collectTime" min-width="70" show-overflow-tooltip>
    </el-table-column>
        <el-table-column label="数据文件路径" prop="collectPath" min-width="250" show-overflow-tooltip>
    </el-table-column>
        <el-table-column label="数据文件名称" prop="collectFilename" min-width="100" show-overflow-tooltip>
    </el-table-column>
        <el-table-column label="采集记录数" prop="collectRecordCount" min-width="50" show-overflow-tooltip>
    </el-table-column>
        <el-table-column label="采集状态" prop="collectStatus" min-width="50" show-overflow-tooltip align="center">
          <template #default="scope">
            <el-tag :type="scope.row.collectStatus ? 'success' : 'danger'">
              {{ scope.row.collectStatus ? '成功' : '失败' }}
            </el-tag>
          </template>
    </el-table-column>
        <el-table-column label="采集处理信息" prop="collectHandleInfo" min-width="100" show-overflow-tooltip>
    </el-table-column>
<!--          <el-table-column fixed="right" label="操作" width="100" align="center">-->
<!--          <template #default="scope">-->
<!--            <el-button-->
<!--                type="primary"-->
<!--                size="small"-->
<!--                link-->
<!--                @click="deleteItem(scope.row.id)"><i-ep-delete />删除</el-button>-->
<!--          </template>-->
<!--        </el-table-column>-->
      </el-table>

      <el-pagination
          :background="true"
          :current-page="pageable.pageNum"
          :page-size="pageable.pageSize"
          :page-sizes="[10, 25, 50, 100]"
          :total="pageable.total"
          layout="total, sizes, prev, pager, next, jumper"
          class="mt-[20px]"
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
      />
    </el-card>

  </div>
</template>

<script setup lang="ts">
  import { useTable,useDownload,useDictionary } from "@/utils/useTable";
  import request from "@/utils/request";

  defineOptions({
    name: "ETLSAPEIDCOLLECTRECORD",
    inheritAttrs: false,
  });

  // 请求参数
  const requestApi = "/pqd-etl/sapeidcollectrecord/queryByPage";

  // 是否需要分页
  const pagination = true;

  const initParam = {};

  // 初始化搜索参数数据
  const searchParamInit = {
    collectTimeSearch: [],
    collectTimeStart: "",
    collectTimeEnd: "",
    collectFilename: "",
    collectStatus: "",
  };

  const deleteParam = {
    api: "/pqd-etl/sapeidcollectrecord/delete",
    data: {},
  };

  // 数据请求错误后处理
  function requestError() {}

  const dataCallBack = ref(undefined); // 无需二次数据处理&不传赞数也行

  // 表格操作 Hooks
  const {
    tableData,
    pageable,
    searchParam,
    ids,
    getTableList,
    search,
    reset,
    handleSizeChange,
    handleCurrentChange,
    handleSelectionChange,
    handleTableDelete,
    loading,
  }: any = useTable(
          requestApi,
          initParam,
          searchParamInit,
          pagination,
          dataCallBack.value,
          requestError,
          deleteParam
  );

  onMounted(() => {
    getTableList();
  });

function getcollectTime(data: any) {
  if (data){
    searchParam.value.collectTimeStart = data[0]
    searchParam.value.collectTimeEnd = data[1]
  }else {
    searchParam.value.collectTimeStart = ""
    searchParam.value.collectTimeEnd = ""
  }
}



async function deleteItem(id: any) {
  ElMessageBox.confirm("确认删除已选中的数据项?", "警告", {
    confirmButtonText: "确定",
    cancelButtonText: "取消",
    type: "warning",
  }).then(async () => {
    const {data} = await request({
      url: `/pqd-etl/sapeidcollectrecord/deleteItem/${id}`,
      method: "post",
    });
    search();
  });
}


  // 导入excel
  const uploadApi = "/pqd-etl/sapeidcollectrecord/upload";
  const excelBox = ref(false);
  const uploadBtn = ref(false);
  const uploadFile = ref("");
  const fileName = ref("");
  const uploadKey = ref(Math.random())

  function excelBoxOpen() {
    excelBox.value = true;
  }

  function getfilepath(e: any) {
    const file = e.target.files[0];
    fileName.value = file.name;
    uploadFile.value = file;
    uploadBtn.value = false;
  }

  function upload() {
    if (!uploadFile.value) {
      ElMessage({
        type: "warning",
        message: "请选择文件！",
      });
      return;
    }

    let formdata = new FormData();
    formdata.append("file", uploadFile.value);
    let config = { headers: { "Content-Type": "multipart/form-data" } };
    request
      .post(uploadApi, formdata, config)
      .then((res) => {
        ElMessage({
          type: "success",
          message: "上传成功！",
        });

        excelBox.value = false;
        uploadBtn.value = true;
        uploadKey.value = Math.random()
        search();
      })
      .catch((error) => {
        console.log(error);
      });
  }

</script>
